package com.hddz.acquisition.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;

import com.hddz.acquisition.R;
import com.hddz.acquisition.model.IViewItem;

public class MeterAdapter<T> extends BaseAdapter implements Filterable {

	private LayoutInflater mInflater;
	private List<String> mListItem;
	private Context mContext;
	private List<IViewItem> mLists;
	private List<IViewItem> mViewLists;
	private Object[] headers = null;
	private int[] headerMiniWidths = null;
	private int[] filters = null;
	private IFilterListener iFilterListener;
	private String lastNoDataChars = "";
	private Boolean nodata = false;

	public MeterAdapter(Context context, List<T> lists, List<T> viewLists, Object[] headers, int[] headerMiniWidths, int[] filters, T t) {
		this(context, lists, viewLists, headers, headerMiniWidths, filters, t, null);
	}

	public MeterAdapter(Context context, List<T> lists, List<T> viewLists, Object[] headers, int[] headerMiniWidths, int[] filters, T t,
			IFilterListener iFilterListener) {
		super();
		this.iFilterListener = iFilterListener;
		this.mContext = context;
		this.mInflater = LayoutInflater.from(context);
		this.mLists = (List<IViewItem>) lists;
		this.mViewLists = (List<IViewItem>) viewLists;
		this.headers = headers;
		this.headerMiniWidths = headerMiniWidths;
		this.filters = filters;
		IViewItem item = (IViewItem) t;
		if (context instanceof Activity) {
			View view = ((Activity) context).findViewById(R.id.ll_list_header_four);
			if (headers.length > 0)
				((TextView) view.findViewById(R.id.item_value_1)).setText(item.getHeaderAdd(context, headers[0]));
			if (headers.length > 1)
				((TextView) view.findViewById(R.id.item_value_2)).setText(item.getHeaderAdd(context, headers[1]));
			if (headers.length > 2)
				((TextView) view.findViewById(R.id.item_value_3)).setText(item.getHeaderAdd(context, headers[2]));
			if (headerMiniWidths.length > 0) {
				if (headerMiniWidths[0] > 0) {
					LayoutParams lp = ((TextView) view.findViewById(R.id.item_value_1)).getLayoutParams();
					lp.width = headerMiniWidths[0];
					((TextView) view.findViewById(R.id.item_value_1)).setLayoutParams(lp);
				} else if (headerMiniWidths[0] < 0)
					((TextView) view.findViewById(R.id.item_value_1)).setVisibility(View.GONE);
			}
			if (headerMiniWidths.length > 1) {
				if (headerMiniWidths[1] > 0) {
					LayoutParams lp = ((TextView) view.findViewById(R.id.item_value_2)).getLayoutParams();
					lp.width = headerMiniWidths[1];
					((TextView) view.findViewById(R.id.item_value_2)).setLayoutParams(lp);
				} else if (headerMiniWidths[1] < 0)
					((TextView) view.findViewById(R.id.item_value_2)).setVisibility(View.GONE);
			}
			if (headerMiniWidths.length > 2) {
				if (headerMiniWidths[2] > 0) {
					LayoutParams lp = ((TextView) view.findViewById(R.id.item_value_3)).getLayoutParams();
					lp.width = headerMiniWidths[2];
					((TextView) view.findViewById(R.id.item_value_3)).setLayoutParams(lp);
				} else if (headerMiniWidths[2] < 0)
					((TextView) view.findViewById(R.id.item_value_3)).setVisibility(View.GONE);
			}
		}
	}

	public List<IViewItem> getViewList() {
		return mViewLists;
	}

	public void updateList(List<T> lists, List<T> viewLists) {
		this.mLists = (List<IViewItem>) lists;
		this.mViewLists = (List<IViewItem>) viewLists;
	}

	public void reflash(ListView list) {
		notifyDataSetChanged();
		list.setSelection(getCount() - 1);
	}

	public void gotoFirst(ListView list) {
		list.setSelection(0);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mViewLists == null ? mLists.size() : mViewLists.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mViewLists == null ? mLists.get(position) : mViewLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_list_data_four, null);
			holder = new Holder();
			holder.mIndex = (TextView) convertView.findViewById(R.id.item_index);
			holder.m_item_value_1 = (TextView) convertView.findViewById(R.id.item_value_1);
			holder.m_item_value_2 = (TextView) convertView.findViewById(R.id.item_value_2);
			holder.m_item_value_3 = (TextView) convertView.findViewById(R.id.item_value_3);
			if (headerMiniWidths.length > 0) {
				if (headerMiniWidths[0] > 0) {
					LayoutParams lp = holder.m_item_value_1.getLayoutParams();
					lp.width = headerMiniWidths[0];
					holder.m_item_value_1.setLayoutParams(lp);
				} else if (headerMiniWidths[0] < 0)
					holder.m_item_value_1.setVisibility(View.GONE);
			}
			if (headerMiniWidths.length > 1) {
				if (headerMiniWidths[1] > 0) {
					LayoutParams lp = holder.m_item_value_2.getLayoutParams();
					lp.width = headerMiniWidths[1];
					holder.m_item_value_2.setLayoutParams(lp);
				} else if (headerMiniWidths[1] < 0)
					holder.m_item_value_2.setVisibility(View.GONE);
			}
			if (headerMiniWidths.length > 2) {
				if (headerMiniWidths[2] > 0) {
					LayoutParams lp = holder.m_item_value_3.getLayoutParams();
					lp.width = headerMiniWidths[2];
					holder.m_item_value_3.setLayoutParams(lp);
				} else if (headerMiniWidths[2] < 0)
					holder.m_item_value_3.setVisibility(View.GONE);
			}
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		IViewItem item = mViewLists == null ? mLists.get(position) : mViewLists.get(position);
		holder.mIndex.setText((position + 1) + "");

		if (headers.length > 0)
			holder.m_item_value_1.setText(item.getValue(headers[0]));
		if (headers.length > 1)
			holder.m_item_value_2.setText(item.getValue(headers[1]));
		if (headers.length > 2)
			holder.m_item_value_3.setText(item.getValue(headers[2]));

		return convertView;
	}

	class Holder {
		private TextView mIndex, m_item_value_1, m_item_value_2, m_item_value_3;
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		/**
		 * A filter object which will filter message key
		 * */
		Filter filter = new Filter() {

			@Override
			protected void publishResults(CharSequence constraint, FilterResults results) {
				mViewLists = (List<IViewItem>) results.values;
				notifyDataSetChanged();
				if (nodata) {
					iFilterListener.noData(constraint.toString());
					Log.d("Log", "iFilterListener.noData");
				}
			}

			@Override
			protected FilterResults performFiltering(CharSequence constraint) {
				FilterResults results = new FilterResults();
				List<IViewItem> FilteredArrList = new ArrayList<IViewItem>();
				nodata = false;
				if (!lastNoDataChars.equals("") && constraint.toString().startsWith(lastNoDataChars)) {
					// do nothing
					results.count = mViewLists.size();
					results.values = mViewLists;
					Log.d("Log", "no change");
				} else {
					if (mListItem == null) {
						mListItem = new ArrayList<String>();
						for (IViewItem item : mLists) {
							if (filters.length > 0) {
								String filter = "";
								for (int resId : filters) {
									filter += item.getValue(resId) + " ";
								}
								mListItem.add(filter);
							} else {
								mListItem.add("");
							}
						}
					}

					if (constraint == null || constraint.length() == 0) {
						results.count = mLists.size();
						results.values = mLists;
					} else {
						constraint = constraint.toString().toLowerCase().trim();

						for (int i = 0; i < mListItem.size(); i++) {
							String data = mListItem.get(i).toLowerCase();
							String[] constraints = constraint.toString().split(" ");
							for (int j = 0; j < constraints.length; j++) {
								if (data.contains(constraints[j])) {
									FilteredArrList.add(mLists.get(i));
									break;
								}
							}
						}
						results.count = FilteredArrList.size();
						results.values = FilteredArrList;
					}
					
					if (results.count == 0) {
						lastNoDataChars = constraint.toString();
					}
					if (results.count == 0 && iFilterListener != null) {
						nodata = true;
					}
					
					Log.d("Log", "results.count :"+results.count );
				}
				// set the Filtered result to return
				if (constraint.toString().equals("")) {
					lastNoDataChars = "";
					Log.d("Log", "lastNoDataChars=" );
				}
				return results;
			}
		};
		return filter;
	}

}
